package com.wh.blockquene;

/**
 * 
 *  Class Name: TestWaitAndNotify.java
 *  Description: 
 *  @author wanghao  DateTime 2016年7月26日 上午11:45:05 
 *  @company hongbo
 *  @email 1228675455@qq.com
 *  
 */
public class TestWaitAndNotify1 {

	public static void main(String[] args) {
		
		final StudentDao dao = new StudentDao();
		
		final Thread thread1 = new Thread() {

			@Override
			public void run() {
				dao.addStudent();
			}
		};
		thread1.start();
		final Thread thread2 = new Thread() {

			@Override
			public void run() {
				dao.deleteStudent();
			}
		};
		thread2.start();
		
	}
	
	static class StudentDao {
		
		
		public void addStudent() {
			synchronized(this) {
				System.out.println("增加开始");
				try {
					System.out.println("线程开始等待！");
					this.wait();
					System.out.println("线程等待结束！");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("增加结束");
			}
		}
		
		public void deleteStudent() {
			synchronized(this) {
				System.out.println("删除开始");
				try {
					Thread.currentThread().sleep(3000);
					this.notify();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("删除结束");
			}
		}
	}
	
}
